package com.bonc.rms.controller.tms;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.service.tms.TalentInformationService;

@Controller
@RequestMapping(value = "/talentInformationController")

public class TalentInformationContorller {
	/**
	 * 部门员工信息
	 * 
	 * @param departmentId
	 * @param
	 * @return
	 */
	
	@Autowired
	private TalentInformationService talentInformationService; 

	@RequestMapping(value = "/talentInformation")
	@ResponseBody
	public String getTalentInformation(
			@RequestParam(value = "pageNum",defaultValue = "0") Integer pageno, 
			@RequestParam(value = "departmentId") Integer departmentId, 
			HttpSession httpSession
			) {
		LogManager.Method("In  contorller : TalentInformationContorller -> getTalentInformation "
				+ "(talentInformationController/talentInformation)");
		
		Map<String, Object> informationMap = new HashMap<String, Object>();
		
		int pageSize = 10;
		
		Integer totalCount = talentInformationService.getTalentInformationCount(departmentId);
		Integer totalPage = totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1);
		
		List<Map<String, Object>> dataList = talentInformationService.getTalentInformation(departmentId, pageno, pageSize);
		
		LogManager.Process("data: "
				+ "\n -- departmentId " + departmentId 
				+ "\n -- pageno " + pageno 
				+ "\n -- pageSize " + pageSize 
				+ "\n -- dataList " + dataList);
		
		informationMap.put("data", dataList);
		informationMap.put("totalNumber", totalCount);
        informationMap.put("prePage", pageno - 1);
        informationMap.put("curPage", pageno);
        informationMap.put("nextPage", pageno + 1);
        informationMap.put("firstPage", 0);
        informationMap.put("lastPage", totalPage - 1);
        informationMap.put("totalPage", totalPage);
        informationMap.put("pageSize", pageSize);
		
        LogManager.Method("Out contorller : TalentInformationContorller -> getTalentInformation "
				+ "(talentInformationController/talentInformation)");
        
		return JSON.toJSONString(informationMap);
	}
	
	@RequestMapping(value = "/searchTalent")
	@ResponseBody
	public String searchTalent(
			@RequestParam(value = "pageNum",defaultValue = "0") Integer pageno, 
			@RequestParam(value = "departmentId", defaultValue = "1") Integer departmentId, 
			@RequestParam(value = "keyWord") String keyWord) {
		LogManager.Method("In  contorller : TalentInformationContorller -> searchTalent "
				+ "(talentInformationController/searchTalent)");
		
		int pageSize = 10;
		
		Map<String, Object> informationMap = new HashMap<String, Object>();
		List<Map<String, Object>> searchRes = talentInformationService.searchTalent(departmentId, keyWord);
		Integer totalCount = searchRes.size();
		Integer totalPage = totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1);
		
		informationMap.put("data", searchRes);
		informationMap.put("totalNumber", totalCount);
        informationMap.put("prePage", pageno - 1);
        informationMap.put("curPage", pageno);
        informationMap.put("nextPage", pageno + 1);
        informationMap.put("firstPage", 0);
        informationMap.put("lastPage", totalPage - 1);
        informationMap.put("totalPage", totalPage);
        informationMap.put("pageSize", pageSize);
		
        LogManager.Method("Out contorller : TalentInformationContorller -> searchTalent "
				+ "(talentInformationController/searchTalent)");
        
		return JSON.toJSONString(informationMap);
	}
}
